package com.res.service.impl;

import com.res.entity.Computer;
import com.res.entity.Readroom;
import com.res.repository.ComputerRepository;
import com.res.service.ComputerService;
import com.res.util.FenyeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class ComputerServiceImpl implements ComputerService {
            @Autowired
            private ComputerRepository computerRepository;
            @Override
            public Page<Computer> findAllComputer(Computer computer,Pageable pageable, Integer rid){

                return  computerRepository.findAll(get(computer,rid),pageable);
    }

    @Override
    public long count(Computer computer,Pageable pageable,Integer rid) {
        return computerRepository.count(get(computer,rid));
    }

    private Specification<Computer> get(Computer computer,Integer rid ) {
        return new Specification<Computer>() {
            @Override
            public Predicate toPredicate(Root<Computer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();
                List<Expression<Boolean>> expressions = predicate.getExpressions();
                if(!StringUtils.isEmpty(computer.getStatus())){
                    expressions.add(criteriaBuilder.equal(root.get("status"),computer.getStatus()));
                }
                if(rid!=null){
                    //root.join 类似于sql中的join链接
                  //  ListJoin<Computer, Readroom> ra=root.join(root.getModel().getList("readRoom",Readroom.class), JoinType.INNER);
                  //  expressions.add(criteriaBuilder.equal(ra.get("rid"),rid));
                    expressions.add(criteriaBuilder.equal(root.get("readRoom").get("rid"),rid));
                }
                return predicate;
            }
        };
    }

    @Override
    public Integer addComputer(Computer computer) {
                try{
                    computerRepository.save(computer);
                    return 1;
                }catch (Exception e){
                    e.printStackTrace();
                    return  0;
                }
    }
    @Override
    public Integer updateComputer(Computer computer) {
        try{
            computerRepository.saveAndFlush(computer);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }
    @Override
    public Integer deleteComputer(Integer cid) {
        try{
            computerRepository.deleteById(cid);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }
}
